/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.edu.amu.wmi.kino.visualjavafx.model.animation.transformations;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import pl.edu.amu.wmi.kino.visualjavafx.model.animation.interpolators.DiscreteInterpolator;
import pl.edu.amu.wmi.kino.visualjavafx.model.animation.interpolators.Interpolation;
import pl.edu.amu.wmi.kino.visualjavafx.model.objects.VisualObject;

/**
 *
 * @author Admin
 */
public class ChangeVisibility implements Transformation{

    // Code block for changeListeners support start
    protected PropertyChangeSupport propertySupport= new PropertyChangeSupport(this);
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.addPropertyChangeListener(listener);
    }
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertySupport.removePropertyChangeListener(listener);
    }
    // Code block for changeListeners support end

    public ChangeVisibility(){
        this.myInterpolation[0]=new DiscreteInterpolator();
    }
    @Override
    public Interpolation getInterpolation() {
        return this.myInterpolation[0];
    }

    @Override
    public void setInterpolation(Interpolation i) {
        Interpolation oldVal = this.myInterpolation[0];
        this.myInterpolation[0]=i;
        propertySupport.firePropertyChange( "Inerpolation" , oldVal, i);
    }

    @Override
    public VisualObject transform(double howMuch, VisualObject transformable,VisualObject orginalTransformedObject) {
        if((transformable instanceof Showable)==false){
            return transformable;
        }

        Showable res = (Showable)transformable;
        double startVal=0.0d;
        if(res.getVisibility()==true)
            startVal=1;
        double endVal=0.0d;
        if(this.getDestinedVisibility()==true)
            endVal=1;

        double visible = this.getInterpolation().getValue(howMuch,startVal,endVal);
        boolean visibility=false;
        if(visible>0)
            visibility=true;

        res.setVisibility(visibility);
        return (VisualObject)res;
    }

    @Override
    public Interpolation[] getInterpolations() {
        return this.myInterpolation;
    }
    public boolean getDestinedVisibility(){
        return this.destinedVisibility;
    }
    public void setDestinedVisibility(boolean val){
        boolean oldVal = this.destinedVisibility;
        this.destinedVisibility=val;
        propertySupport.firePropertyChange( "destinedVisibility" , oldVal, val);

    }
    private Interpolation[] myInterpolation=new Interpolation[1];
    private boolean destinedVisibility=true;

}
